package com.wnhz.work.yz.fragment.sell;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ServiceProcessActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ProgressFindBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.x;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/23 0023.
 */

public class ProgressFindFragment extends Fragment {

    private View view;

    private Context context;
    private ListView listView;

    public void setContext(Context context) {
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_progress_found, null);
        initView();
        return view;


    }

    private void initView() {
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        listView = (ListView) view.findViewById(R.id.listView);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        upLoad();
    }

    List<ProgressFindBean.InfoEntity> info = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.ORDER_SERVICELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                Log.e("----", result);
                ProgressFindBean progre = gson.fromJson(result, ProgressFindBean.class);
//                bean = gson.fromJson(result, SellAfterApplyBean.class);

                if ("1".equals(progre.getResult())) {
                    info = progre.getInfo();
                }
//                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                super.onFinished();
                handler.sendEmptyMessage(1);
            }
        });

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            initData2();
            super.handleMessage(msg);
        }
    };

    private void initData2() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return info.size();
            }

            @Override
            public Object getItem(int position) {
                return info.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Holder holder;
                if (convertView == null) {
                    holder = new Holder();
                    convertView = View.inflate(context, R.layout.progress_find, null);
                    holder.tv_good_code = (TextView) convertView.findViewById(R.id.tv_good_code);
                    holder.state = (TextView) convertView.findViewById(R.id.state);
                    holder.tv_good_name = (TextView) convertView.findViewById(R.id.tv_good_name);
                    holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                    holder.progress_find = (Button) convertView.findViewById(R.id.progress_find);
                    holder.iv_goods = (ImageView) convertView.findViewById(R.id.iv_goods);
                    convertView.setTag(holder);
                } else {
                    holder = (Holder) convertView.getTag();
                }
                holder.tv_good_code.setText(info.get(position).getService_num());
                holder.state.setText(info.get(position).getStatus());
                holder.tv_good_name.setText(info.get(position).getGoods_name());
                holder.tv_time.setText(info.get(position).getAdd_time());
                x.image().bind(holder.iv_goods, info.get(position).getLogo_pic());
                final ProgressFindBean.InfoEntity infoEntity = info.get(position);
                final String serviceId = info.get(position).getService_id();
                holder.progress_find.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getContext(), ServiceProcessActivity.class);
                        intent.putExtra("infoItem", infoEntity);
                        intent.putExtra("serviceId", serviceId);
                        startActivity(intent);
                    }
                });
                return convertView;
            }

            class Holder {
                TextView tv_good_code, state, tv_good_name, tv_time;
                Button progress_find;
                ImageView iv_goods;
            }
        });
    }

    private RecyclerView recycler;

    private void initData() {
        Log.e("----------", info.size() + "");
        recycler.setAdapter(new BaseRVAdapter(context, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.progress_find;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.tv_good_code, info.get(position).getService_num());
                holder.setTextView(R.id.state, info.get(position).getStatus());
                x.image().bind(holder.getImageView(R.id.iv_goods), info.get(position).getLogo_pic());
                holder.setTextView(R.id.tv_good_name, info.get(position).getGoods_name());
                holder.setTextView(R.id.tv_time, "申请时间：" + info.get(position).getAdd_time());
                final ProgressFindBean.InfoEntity infoEntity = info.get(position);
                holder.getView(R.id.progress_find).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getContext(), ServiceProcessActivity.class);
                        intent.putExtra("infoItem", infoEntity);
                        startActivity(intent);
                    }
                });
            }

        });
    }

    @Override
    public void onDestroy() {
        if (handler != null)
            handler.removeCallbacks(null);
        super.onDestroy();

    }

}
